/*
 * @Author: Cheng Wenxing
 * @Date: 2020-10-25 19:23:43
 * @Last Modified by: 程文星
 * @Last Modified time: 2020-10-26 21:38:29
 */
import request from '@utils/request';
import * as actionTypes from './actionType';
import {message}from 'antd';
import moment from 'moment'
import { getAllByAltText } from '@testing-library/react';

//  所有告警信息和摄像头信息
export const getAllDataRequest = () => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warningDataStatistics/getAllData', {
            cmd: 'getAllData',
            type: 'request',
            request: {}
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let result = res.message;
                let todayWarning = result.todayWarningStatistics;
                let alarmStatistics = result.historyWarningStatistics;
                let regionProportion = result.warningRegionProportion;
                let latestTenWarning = modifyingtimestamps(result.latestTenWarning);
                let monthWarning = result.monthWarningStatistics;
                let payload = result.vidiconData;

                dispatch(getAllData(todayWarning,alarmStatistics,regionProportion,latestTenWarning,monthWarning,payload))
            } else {
                message.error('今日告警获取失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const getAllData= (todayWarning,alarmStatistics,regionProportion,latestTenWarning,monthWarning,payload) => ({
    type: actionTypes.ALLDATA,
    todayWarning,
    alarmStatistics,
    regionProportion,
    latestTenWarning,
    monthWarning,
    payload
})

//今日告警
export const getodayWarningStatisticsRequest = () => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warningDataStatistics/todayWarningStatistics', {
            cmd: 'todayWarningStatistics',
            type: 'request',
            request: {}
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let result = res.message;
                dispatch(getodayWarningUpdateReducer(result))
            } else {
                message.error('今日告警获取失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const getodayWarningUpdateReducer= (todayWarning) => ({
    type: actionTypes.TODAYWARING,
    todayWarning
})
//告警占比
export const getwarningRegionProportionRequest = () => {
    return (dispatch) => {
        request('POST','/api/server/web/rest/warningDataStatistics/warningRegionProportion', {
            cmd: 'warningRegionProportion',
            type: 'request',
            request: {}
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let result = res.message;
                dispatch(getwarningRegionProportionUpdateReducer(result))
            } else {
                message.error('告警占比获取失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const getwarningRegionProportionUpdateReducer= (regionProportion) => ({
    type: actionTypes.WARININGPROPORTION,
    regionProportion
})
//告警统计
export const getAlarmStatisticsRequest = () => {
    return (dispatch) => {
        request('POST','/api/server/web/rest/warningDataStatistics/historyWarningStatistics', {
            cmd: 'historyWarningStatistics',
            type: 'request',
            request: {}
        }).then(obj => {
            let res = obj.response;
           if (res.res) {
                let result = res.message;
                dispatch(getAlarmStatisticsUpdateReducer(result))
            } else {
                message.error('告警统计获取失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const getAlarmStatisticsUpdateReducer = (alarmStatistics) => ({
    type: actionTypes.WARININGSTATISTICS,
    alarmStatistics
})
//最近告警
export const getatestTenWarningRequest = () => {
    return (dispatch) => {
        request('POST','/api/server/web/rest/warningDataStatistics/latestTenWarning', {
            cmd: 'latestTenWarning',
            type: 'request',
            request: {}
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let result = res.message;
                let data=modifyingtimestamps(result) 
                dispatch(getmonthWarningUpdateReducer(data))
            } else {
                message.error('最近10天告警数据获取失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const getmonthWarningUpdateReducer = (latestTenWarning) => ({
    type: actionTypes.LATESTENWARING,
    latestTenWarning
})
//将时间戳改成正常时间
const modifyingtimestamps =(data)=>{
    let array =data

    for(var i=0;i<data.length;i++){
        array[i].warningTime=moment(data[i].warningTime).format('YY-MM-DD')
    }
    return array
}

//告警趋势
export const getmonthWarningStatisticsRequest = () => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warningDataStatistics/monthWarningStatistics', {
            cmd: 'monthWarningStatistics',
            type: 'request',
            request: {}
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let result = res.message;
                let data=timestamps(result)
                dispatch(WarningUpdateReducer(data))
            } else {
                message.error('最近十五天告警数据获取失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const WarningUpdateReducer = (monthWarning) => ({
    type: actionTypes.MONTHWARNING,
    monthWarning
})
//将时间戳改成正常时间
const timestamps =(data)=>{
    let array =data

    for(var i=0;i<data.length;i++){
        array[i].time=moment(data[i].time).format('YYYY-MM-DD')
    }
    return array
}
//注销登录
export const logoutRequest = (params) => {
    // const IP_URL = process.env.REMOTE_SERVER_IP + params.api
    return (dispatch) => {
          request("POST", '/api/server/web/rest/user/logout', {
                cmd: "logout",
                type: "request",
                request: {

                }
          }).then(response => {
                const result = response.response;
                if (result.res) {
                      message.success('账号注销成功', 1);
                      sessionStorage.clear();
                      dispatch(logoutReducer(result.message))
                } else {
                      message.error('账号注销失败' + result.exception, 1.5);
                }
          }).catch(error => {
                console.log(error)
          });
    };
};

//账号注销
const logoutReducer = (result) => ({
    type: actionTypes.LOGOUT,
    result
})
